package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.Vo.EduCourseVo;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.CourseVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author kaixuan
 * @since 2020-05-30
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin //前后端分离解决跨域访问
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页数据列表")
    @GetMapping("pageTeacher/{current}/{size}")
    public R pageList(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                      @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable long size)
    {
        Page<EduCourse> page = new Page<EduCourse>(current,size);
        courseService.page(page,null);
        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    /**
     * 待条件查询分页数据
     * @param current
     * @param size
     * @param courseQuery
     * @return
     */
    @ApiOperation(value = "分页数据列表")
    @PostMapping("pageCourseCondition/{current}/{size}")
    public R pageCourse(@PathVariable long current, @PathVariable long size, @RequestBody CourseQuery courseQuery){
        Page<EduCourse> page = new Page<>(current,size);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(courseQuery.getTitle())){
            queryWrapper.like("title",courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getStatus())){
            queryWrapper.like("status",courseQuery.getStatus());
        }
        queryWrapper.orderByDesc("gmt_create");
        courseService.page(page,queryWrapper);
        long total = page.getTotal();
        List<EduCourse> list = page.getRecords();
        return R.ok().data("rows",list).data("total",total);
    }

    /**
     * 根据id逻辑删除课程 -- 描述 -- 小节 --视频
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id逻辑删除课程")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id",value = "课程id",required = true)@PathVariable String id){
        boolean flag = courseService.removeAllData(id);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     * 根据id编辑课程信息
     * @param eduCourse
     * @return
     */
    @ApiOperation(value = "编辑课程信息")
    @PostMapping("updateById")
    public R updateById(@RequestBody EduCourse eduCourse){
        boolean flag = courseService.updateById(eduCourse);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     * 添加课程信息
     * @param courseVo
     * @return
     */
    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("saveCourseInfo")
    public R save(@RequestBody CourseVo courseVo){
        String id = courseService.saveCourseInfo(courseVo);
        return R.ok().data("courseId",id);
    }

    /**
     * 根据课程id查询数据
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id查询数据")
    @GetMapping("getCourse/{courseId}")
    @ResponseBody
    public R getCourse(@ApiParam(value = "课程id",name = "courseId",required = true) @PathVariable String courseId){
        Map<String,Object> map = courseService.getCourseVo(courseId);
        CourseVo courseVo = (CourseVo)map.get("courseVo");
        List<OneSubject> items = (List<OneSubject>)map.get("items");
        List<EduTeacher> teacherList = (List<EduTeacher>)map.get("teacherList");
        return R.ok().data("courseVo",courseVo).data("items",items).data("teacherList",teacherList);
    }
    /**
     * 添加课程基本信息
     * @param courseVo
     * @return
     */
    @ApiOperation(value = "修改课程基本信息")
    @PostMapping("updateCourse")
    public R updateCourse(@RequestBody CourseVo courseVo){
        String courseId = courseService.updateCourseVo(courseVo);
        return R.ok().data("courseId",courseId);
    }

    /**
     * 根据课程id获取全部关联数据
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id获取全部关联数据")
    @GetMapping("getCoursePublishVo/{courseId}")
    public R getCoursePublishVo(@ApiParam(value = "课程id",name = "courseId",required = true)@PathVariable String courseId){
        Map<String,Object> map = courseService.getCoursePublishVo(courseId);
        CoursePublishVo coursePublishVo = (CoursePublishVo)map.get("coursePublishVo");
        List<OneSubject> subjectList = (List<OneSubject>)map.get("items");
        return R.ok().data("coursePublishVo",coursePublishVo).data("items",subjectList);
    }

    /**
     * 根据课程id最终发布 更改发布状态status
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id最终发布 更改发布状态status")
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@ApiParam(value = "课程id",name = "courseId",required = true)@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");//设置发布
        boolean flag = courseService.updateById(eduCourse);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     * 根据课程id查询数据
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id查询数据")
    @GetMapping("getCourseVo/{courseId}")
    public EduCourseVo getCourseInfo(@ApiParam(value = "课程id",name = "courseId",required = true) @PathVariable String courseId){
        EduCourse course = courseService.getById(courseId);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(course,eduCourseVo);
        EduTeacher teacher = teacherService.getById(course.getTeacherId());
        eduCourseVo.setName(teacher.getName());
        return eduCourseVo;
    }
}

